#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <net/if_arp.h>
#include <net/if.h>
#include <linux/rtnetlink.h>
#include <linux/netlink.h>
#include <netinet/ether.h>
#include <netinet/in.h>
#include <asm/types.h>
#include <arpa/inet.h>
// #include <ifaddrs.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>



uint64_t util_current_utc_time_ms(void)
{
    struct timeval tv;

    gettimeofday(&tv, NULL);

    return ((uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000);
}

int util_ifname_mac_address(const char *ifname, char *mac)
{
	int fd = -1;
	struct ifreq if_hwaddr;

	fd = socket(PF_INET, SOCK_STREAM, 0);
	if (fd < 0) {
		return -1;
	}

    memset(&if_hwaddr, 0, sizeof(if_hwaddr));
    strncpy(if_hwaddr.ifr_name, ifname, sizeof(if_hwaddr.ifr_name));

    int ret = ioctl(fd, SIOCGIFHWADDR, &if_hwaddr);
    if (ret >= 0) {
        unsigned char* hwaddr = (unsigned char *)if_hwaddr.ifr_hwaddr.sa_data;
        sprintf(mac, "%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
            		hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
    }

	close(fd);

	return ret;
}

void get_current_time(uint64_t* tnow)
{
     struct timeval tv;
     gettimeofday(&tv, NULL);             
     *tnow = ((uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000);
}
